#include "Comm.hpp"
#include "Fifo.hpp"
#include <unistd.h>
int main()
{
    // 1.获取key
    int shmkey = GetShmKeyOrDie();
    cout << "key: " << ToHex(shmkey) << endl;

    // 2.申请共享内存
    int shmid = CreateShm(shmkey, defaultsize);
    cout << "shmid: " << shmid << endl;
    //sleep(2);

    // ShmDebug(shmid);

    // 3.挂接shm
    char *addr = (char *)ShmAttach(shmid);
    cout  << "shm attach success, " << "addr: " << ToHex((uint64_t)addr) << endl;
    memset(addr, 0, defaultsize);
    // sleep(5);

    // 0.引入管道
    Fifo fifo;
    Sync syn;
    syn.OpenReadOrDie();

    // 可以进行通信了
    for (;;)
    {
        if (!syn.Wait()) // 客户端唤醒一次，服务端读取一次
        break;
        cout << "shm content: " << addr << endl;
    }

    // 4.去关联
    ShmDetach(addr);
    cout << "shm detach success, addr: " << ToHex((uint64_t)addr) << endl;
    // sleep(10);

    // 5.删除shm
    DeleteShm(shmid);
    cout << "shmctl delete success" << "shmid: " << shmid << endl;
}